/**
******************************************************************************
* @file    bsp_clock.c 
* @author  jobs
* @version v0.00
* @date    2021-03-04
* @brief   
* @note    
*
******************************************************************************
*/

#include <stdint.h>
#include <stdbool.h>

#include "fsl_power.h"
#include "fsl_clock.h"

#include "bsp_clock.h"

/* System clock frequency. */
extern uint32_t SystemCoreClock;

void main_clock_init(void)
{
  /*!< Set up the clock sources */
  /*!< Set up FRO */
  POWER_DisablePD(kPDRUNCFG_PD_FRO_EN); /*!< Ensure FRO is on  */
  CLOCK_SetupFROClocking(12000000U);    /*!< Set up FRO to the 12 MHz, just for sure */
  CLOCK_AttachClk(kFRO12M_to_MAIN_CLK); /*!< Switch to FRO 12MHz first to ensure we can change voltage without
                                             accidentally being below the voltage for current speed */
  POWER_SetVoltageForFreq(150000000U);                               /*!< Set voltage for the one of the fastest clock outputs: System clock output */
  CLOCK_SetFLASHAccessCyclesForFreq(150000000U); /*!< Set FLASH wait states for core */

  /*!< Set up PLL */
  CLOCK_AttachClk(kFRO12M_to_SYS_PLL); /*!< Switch PLL clock source selector to FRO12M */
  const pll_setup_t pllSetup = {.syspllctrl = SYSCON_SYSPLLCTRL_UPLIMOFF_MASK | SYSCON_SYSPLLCTRL_DIRECTO_MASK,
                                .syspllndec = SYSCON_SYSPLLNDEC_NDEC(11U),
                                .syspllpdec = SYSCON_SYSPLLPDEC_PDEC(2U),
                                .syspllssctrl = {0x0U, (SYSCON_SYSPLLSSCTRL1_MD(76800U) | (uint32_t)(kSS_MF_512) |
                                                        (uint32_t)(kSS_MR_K0) | (uint32_t)(kSS_MC_NOC))},
                                .pllRate = 150000000U,
                                .flags = PLL_SETUPFLAG_POWERUP};
  CLOCK_SetPLLFreq(&pllSetup); /*!< Configure PLL to the desired values */

  /* PLL in Fractional/Spread spectrum mode */
  /* SYSTICK is used for waiting for PLL stabilization */

  SysTick->LOAD = 111999UL;                /*!< Set SysTick count value */
  SysTick->VAL = 0UL;                      /*!< Reset current count value */
  SysTick->CTRL = SysTick_CTRL_ENABLE_Msk; /*!< Enable SYSTICK */
  while ((SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) != SysTick_CTRL_COUNTFLAG_Msk)
  {
  }                    /*!< Waiting for PLL stabilization */
  SysTick->CTRL = 0UL; /*!< Stop SYSTICK */

  /*!< Set up dividers */
  CLOCK_SetClkDiv(kCLOCK_DivAhbClk, 1U, false); /*!< Set AHBCLKDIV divider to value 1 */

  /*!< Set up clock selectors - Attach clocks to the peripheries */
  CLOCK_AttachClk(kSYS_PLL_to_MAIN_CLK); /*!< Switch MAIN_CLK to SYS_PLL */
  SYSCON->MAINCLKSELA =
      ((SYSCON->MAINCLKSELA & ~SYSCON_MAINCLKSELA_SEL_MASK) |
       SYSCON_MAINCLKSELA_SEL(0U)); /*!< Switch MAINCLKSELA to FRO12M even it is not used for MAINCLKSELB */
  /*!< Set SystemCoreClock variable. */
  SystemCoreClock = BOARD_BOOTCLOCKPLL150M_CORE_CLOCK;
}

/******************************** END OF FILE *********************************/
